package com.bnyy.medicalHousekeeper.moudle.message.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.message.bean.ContactUser;

import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/9
 **/
public class ChatContactAdapter extends BaseNormalListAdapter<ContactUser, ChatContactAdapter.ViewHolder> {

    private int type;
    private Callback callback;

    public ChatContactAdapter(Context context, int type, Consumer<ContactUser> consumer, Callback callback) {
        super(context, consumer);
        this.type = type;
        this.callback = callback;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_chat_contact, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        ContactUser contactUser = getData(position);

        GlideHelper.setCircleImage(mContext, contactUser.getUser_img(), holder.iv, R.mipmap.icon_default_header_man);

        holder.tvName.setText(contactUser.getUsername());
        switch (type) {
            case 1:
                holder.tvDesc.setText("关注了你" + contactUser.getDatetime().split(" ")[0]);
                break;
            case 2:
                holder.tvDesc.setText("通讯录好友");
                break;
            case 3:
                break;
        }
        holder.tvDesc.setText(contactUser.getDesc());
        if (type == 1) {
            holder.tvDesc.append(contactUser.getDatetime().split(" ")[0]);
        }

        if (contactUser.getStatus() == 1) {
            holder.tvRemove.setVisibility(View.GONE);
            holder.tvFocus.setVisibility(View.GONE);
            holder.tvFocused.setVisibility(View.VISIBLE);
            holder.tvFocused.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null) {
                        callback.focusOrNot(position);
                    }
                }
            });
        } else {
            holder.tvRemove.setVisibility(View.VISIBLE);
            holder.tvRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null) {
                        callback.remove(position);
                    }
                }
            });
            holder.tvFocus.setVisibility(View.VISIBLE);
            holder.tvFocus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null) {
                        callback.focusOrNot(position);
                    }
                }
            });
            holder.tvFocused.setVisibility(View.GONE);
        }
    }

    public static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        ImageView iv;
        TextView tvName;
        TextView tvDesc;
        TextView tvRemove;
        TextView tvFocus;
        TextView tvFocused;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            iv = itemView.findViewById(R.id.iv);
            tvName = itemView.findViewById(R.id.tv_name);
            tvDesc = itemView.findViewById(R.id.tv_desc);
            tvRemove = itemView.findViewById(R.id.tv_remove);
            tvFocus = itemView.findViewById(R.id.tv_focus);
            tvFocused = itemView.findViewById(R.id.tv_focused);
        }
    }

    public interface Callback {
        void remove(int position);

        void focusOrNot(int position);
    }
}





























